package com.etooling.framework.controller;

import com.etooling.framework.entity.ImportDataBatch;
import com.etooling.framework.jpa.JpaQueryBuilder;
import com.etooling.framework.jpa.Specifications;
import com.etooling.framework.repository.ImportDataBatchRepository;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import com.etooling.pdm.entity.Bom;
import com.etooling.pdm.entity.GoodsSpecs;
import com.etooling.pdm.repository.BomRepository;
import com.etooling.pdm.service.BomService;
import com.etooling.pdm.thread.UploadBomExcelThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author dyz
 * @date 2023/4/12 10:20
 * @description GoodsSpecsController
 */
@RequestMapping(value = "/admin/importDataBatch")
@Controller
@EnableAutoConfiguration
public class ImportDataBatchController {

    @Autowired
    ImportDataBatchRepository importDataBatchRepository;
    @Autowired
    BomService bomService;
    @Autowired
    BomRepository bomRepository;


    /**
     * 按照类别进行查询导入记录
     *
     * @param request
     * @param pageSize
     * @param page
     * @param typeList
     * @return
     */
    @RequestMapping("impDataBachPageQuery")
    @ResponseBody
    ReplyObj impDataBachPageQueryimpDataBachPageQuery(HttpServletRequest request,
                                                      @RequestParam(defaultValue = "15", required = false, value = "pageSize") int pageSize, //请求参数
                                                      @RequestParam(defaultValue = "1", required = false, value = "page") int page,
                                                      @RequestParam(defaultValue = "", required = false, value = "typeList") List<String> typeList
    ) {
        JpaQueryBuilder pb = Specifications.<ImportDataBatch>and();
        String companyId = WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
        pb.eq("uploadCompanyId", companyId);
        if (WebUtils.checkStr(typeList) && typeList.size() > 0) {
            pb.in("fileType", typeList);
        }
        //排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");//使用一个排序
        Specification<ImportDataBatch> importdatabatch = pb.build();
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);
        return WebUtils.flushSuccess(importDataBatchRepository.findAll(importdatabatch, pageable));
    }


    /**
     * 重新提交导入文件
     *
     * @param request
     * @param importDataBatchId
     * @return
     */
    @RequestMapping("reSubmitBomExcelData")
    @ResponseBody
    ReplyObj reSubmitBomExcelData(HttpServletRequest request,
                                  @RequestParam(defaultValue = "", required = false, value = "importDataBatchId") String importDataBatchId
    ) {

        String companyId = WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
        String loginId = WebUtils.getSessionValue(request, WebUtils.LOGIN_USER_KEY);

        JpaQueryBuilder pb = Specifications.<Bom>and();
        pb.eq("batchId", importDataBatchId);
        pb.eq("companyId", companyId);
        List<Bom> all = bomRepository.findAll(pb.build());
        if (all.size() < 1) {
            return WebUtils.flushFailure("找不到BOM记录");
        }
        Bom bom = all.get(0);
        String bomName = bom.getBomName();
        String bomMemo = bom.getMemo();

        ImportDataBatch importDataBatch = importDataBatchRepository.getOne(importDataBatchId);
        importDataBatch.setCreateTime(new Date());
        importDataBatch.setDoneTime(null);
        importDataBatch.setDataCnt(null);
        importDataBatch.setImplMemo(null);
        importDataBatch.setErrorMemo(null);
        importDataBatch.setStatus(ImportDataBatch.STATUS_SUBMIT_DOING);
        ImportDataBatch save = importDataBatchRepository.save(importDataBatch);


        // 创建并启动线程
        UploadBomExcelThread uploadBomExcelThread = new UploadBomExcelThread(bomService, save, loginId, companyId, bomName, bomMemo);
        uploadBomExcelThread.start();
        return WebUtils.flushSuccess();
    }


}
